package mycollections;

import java.awt.List;

public class MyArrayList<E> extends List{

	
	private static final long serialVersionUID = 1L;
        private Object elementData[];
        private int capacity;
        private int size;
        
        public MyArrayList(){
            capacity = 10;
            elementData = (E[])new Object[capacity];
        }
        
        public MyArrayList( int capacity){
            this.capacity = capacity;
            elementData = (E[])new Object[capacity];
        }
                
        public void add( Object value ){
            ensureCapacity(size + 1);
            elementData[size] = value;
            size++;
        }
        
        public void add ( int index, Object value){
            ensureCapacity (size + 1);
            System.arraycopy(elementData, index, elementData, index + 1, size - index);
            elementData[index] = value;
            size++;
            
        }
        
             
        public void ensureCapacity( int elemNumber ){
            if ( capacity < elemNumber ){
                Object[] oldData = (E[]) new Object[capacity];
                System.arraycopy(elementData, 0, oldData, 0, capacity);
                capacity = (capacity * 3) / 2 + 1;
                elementData = (E[]) new Object[capacity];
                System.arraycopy(oldData, 0, elementData, 0, capacity);
            }
        }
        
        public String toString(){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append( getClass().getName() + " ");
            for ( int i = 0; i < size; i++ ){
                stringBuilder.append( elementData[i] + " ");
            }
            stringBuilder.append("\n");
            return stringBuilder.toString();
        }
        
        public boolean contains(Object o) {
            return indexOf(o) >= 0;
        }

    
        public int indexOf(Object o) {
            if (o == null) {
                for (int i = 0; i < size; i++)
                    if (elementData[i]==null)
                        return i;
            } else {
                for (int i = 0; i < size; i++)
                    if (o.equals(elementData[i]))
                        return i;
            }
            return -1;
    }
        
        public boolean contains(){
            return false;
        }
}
